/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.appleframework.templates;

import java.util.HashMap;
import java.util.Map;
import org.appleframework.Apple;
import org.appleframework.Apple.Mode;
import org.appleframework.exceptions.TemplateNotFoundException;
import org.appleframework.vfs.FileSystem;

/**
 *
 * @author zhujg
 */
public class TemplateLoader {

  protected static Map<String, Template> templates = new HashMap<String, Template>();

  public static Template load(FileSystem file) {
    if (!templates.containsKey(file.relativePath())) {
      templates.put(file.relativePath(), TemplateCompiler.compiler(file));
    }
    Template template = templates.get(file.relativePath());
    if (Mode.DEV == Apple.mode && template.timestamp < file.lastModified()) {
      templates.put(file.relativePath(), TemplateCompiler.compiler(file));
    }
    template = templates.get(file.relativePath());
    if(template == null){
       throw new TemplateNotFoundException(file.relativePath());
    }
    return template;
  }

  public static Template load(String path) {
    FileSystem file = Apple.templatesPath.child(path);
    Template template = load(file);
    return template;
  }
}
